package org.docmanagement.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.docmanagement.commons.struts.CommonAction;
import org.docmanagement.data.Configuration;
import org.docmanagement.forms.ConfigurationForm;

/**
 * Created on 12 nov. 2004
 * @author eveno
 */
public class ConfigureAction extends CommonAction
{
    /**
     * 
     */
    public ConfigureAction()
    {
        super();
    }
    protected void createMethodMap()
    {
        methodMap = new HashMap<String, String>();
        methodMap.put("Lister", "list");
        methodMap.put("Enregistrer", "save");
    }
    /**
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
        ConfigurationForm configForm = (ConfigurationForm) form;
        
        Configuration configuration = getConfigurationService().getConfiguration();
        if (configuration == null)
            configuration = new Configuration();
        log.debug("Current Configuration: "+configuration);
        getConfigurationService().setConfiguration(toConfiguration(configForm, configuration));
        log.debug("New Configuration: "+configuration);

        addConfiguration(request, getConfigurationService().getConfiguration());

        return mapping.findForward(SUCCESS);
	}
    /**
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
        addConfiguration(request, getConfigurationService().getConfiguration());
        return mapping.findForward(SUCCESS);
	}
    /**
     * @param request
     * @param prefs
     */
    private void addConfiguration(HttpServletRequest request, Configuration configuration)
    {
        try
        {
            ConfigurationForm configForm = new ConfigurationForm();
            BeanUtils.populate(configForm, BeanUtils.describe(configuration));
            log.info("repository.path="+configForm.getRepositoryPath());
            request.setAttribute(CONFIGURATION_KEY, configForm);
        }
        catch (Exception e)
        {
            log.error("Can't populate ConfigurationForm from Configuration, "+configuration, e);
            throw new RuntimeException("Can't populate Configuration from ConfigurationForm, "+configuration, e);
        }
    }

    public Configuration toConfiguration(ConfigurationForm configurationForm, Configuration configuration)
    {
        try
        {
	        BeanUtils.populate(configuration, BeanUtils.describe(configurationForm));
	        return configuration;
        }
        catch (Exception e)
        {
            log.error("Can't populate Configuration from ConfigurationForm, "+configurationForm, e);
            throw new RuntimeException("Can't populate Configuration from ConfigurationForm, "+configurationForm, e);
        }
    }
}
